<?php
    class ControllerManage{
        public $sController = 'home';
        public $sAction = 'index';
        public $sParam = '';
        public $aParams = array();
        public $sId = '';
        public $sPath = '';
        public $sAlias = '';
        
        public $oController = null;

        public function __construct($sParam = '')
        {
            core::checkSession();
            $this->sParam = trim($sParam,"/");
            $aStrs = explode("/",$this->sParam);
            $bAdmin = false;
            if(isset($aStrs[0]) && !empty($aStrs[0]))
            {
                if($aStrs[0] == 'admin')
                {
                    $bAdmin = true;
                    define('IS_ADMIN_PANEL',true);
                    if(isset($aStrs[1]))
                    {
                        $this->sController = $aStrs[1];
                    }
                }
                else
                {
                    $this->sController = $aStrs[0];
                }
            }
            if(isset($aStrs[1]))
            {
                $this->sAction = (!$bAdmin ? $aStrs[1] : (isset($aStrs[2]) ? $aStrs[2] : 'index')) ;
            }
            if(isset($aStrs[2]))
            {
                $this->sId = (!$bAdmin ? $aStrs[2] : (isset($aStrs[3]) ? $aStrs[3] : '')) ;
            }
            if(isset($aStrs[3]))
            {
                $this->sAlias =(!$bAdmin ? $aStrs[3] : (isset($aStrs[4]) ? $aStrs[4] : '')) ;
            }
            $this->sPath = ($bAdmin ? 'admin/' : '').$this->sController;
            $this->sPath = $this->sPath."/".$this->sAction;
        }

        public function getHeader()
        {
            echo '<script type="text/javascript">';
            echo 'CORE = {};';
            echo 'CORE.sController = "'.$this->sController.'";';
            echo 'CORE.sAction = "'.$this->sAction.'";';
            echo 'CORE.sBaseUrl = "'.BASE_URL.'";';
            echo 'CORE.sId = "'.$this->sId.'";';
            if(database::isUser())
            {
                echo 'CORE.isLogin = true;';
            }
            else
            {
                echo 'CORE.isLogin = false;';
            }
            echo '</script>';
        }

        public function getController()
        {
            if(!@file_exists("controller/".$this->sPath.".php") ) 
            {
                echo "<h1>PAGE NOT FOUND!</h1>";
            }
            else
            {
                include_once("controller/".$this->sPath.".php");
                $aParams = array(
                    'controller' => $this->sController,
                    'action' => $this->sAction,
                    'id' => $this->sId,
                    'alias' => $this->sAlias
                );
                eval('$template = new '.$this->sController.'_'.$this->sAction.'_Controller($aParams);');
                eval('$template->process();');
                include("template/".$this->sPath.".html.php");
            }
        }
        
        public function getContent()
        {
            if($this->oController)
            {
                $template = $this->oController;
                core::handleMessage();
                include("template/".$this->sPath.".html.php");
            }
            else
            {
                echo "<h1>PAGE NOT FOUND!</h1>";
            }
        }
        
        public function run()
        {
            if(!@file_exists("controller/".$this->sPath.".php") ) 
            {
                
            }
            else
            {
                include_once("controller/".$this->sPath.".php");
                $aParams = array(
                    'controller' => $this->sController,
                    'action' => $this->sAction,
                    'id' => $this->sId,
                    'alias' => $this->sAlias
                );
                eval('$this->oController = new '.$this->sController.'_'.$this->sAction.'_Controller($aParams);');
                eval('$this->oController->process();');
            }
        }
    }
?>
